import datetime
from email import encoders  # 转码
from email.mime.base import MIMEBase  # 附件
from email.mime.multipart import MIMEMultipart
import smtplib
import os
from common.load_cfg import load_sender_info

class send_mail:
    def __init__(self, attachment_dir, filename, subject, receiver):
        self.filepath = os.path.join(attachment_dir, filename)
        self.filename = filename
        self.subject = subject
        self.receiver = receiver

    def send_mail_info(self):
        self.sender = "release@jieshun.cn"
        self.smtpserver = "smtp.exmail.qq.com"
        self.username = 'release@jieshun.cn'
        self.password = 'Ywzx.0326'
        self.msg = MIMEMultipart()
        self.msg['From'] = self.sender
        self.msg['To'] = ','.join(self.receiver)
        self.msg['Subject'] = self.subject
        with open(self.filepath, 'rb') as f:
            mime = MIMEBase('xlsx', 'xlsx', filename = self.filepath)
            mime.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', self.filename))
            mime.add_header('Content-ID', '<0>')
            mime.add_header('X-Attachment-Id', '0')
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
        self.msg.attach(mime)

        try:
            self.login_and_send()
        except Exception as e:
            print(e)
            #notify_me(e)

    def login_and_send(self):
        smtp = smtplib.SMTP_SSL(self.smtpserver, 465)
        smtp.login(self.username, self.password)
        smtp.sendmail(self.username, self.receiver, self.msg.as_string())

if __name__ == "__main__":
    file_path = ""
    file_name = ""
    subject = ""
    receiver = ""
    send_mail(file_path, file_name, subject, receiver)